from django.db import models
from django.contrib.auth.models import User

class Form(models.Model):
    DURATION_UNIT_CHOICES = (
        ('H', 'Hours'),
        ('D', 'Days'),
        ('W', 'Weeks'),
        ('M', 'Months'),
    )
    author = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    description = models.TextField(max_length=200, blank=True)
    keywords = models.CharField(max_length=100, blank=True)
    published = models.BooleanField()
    start_pub = models.DateTimeField('start publishing', blank=True, null=True)
    end_pub = models.DateTimeField('end publishing', blank=True, null=True)
	
    duration = models.FloatField(blank=True, null=True)
    duration_unit = models.CharField(max_length=1, choices=DURATION_UNIT_CHOICES, blank=True, null=True)
    
    ranking = models.IntegerField(default=1) # Used to display results
    
    def __unicode__(self):
        return self.title

class Field(models.Model):
    TYPE_CHOICES = (
        ('T', 'Text'),
        ('A', 'Textarea'),
        ('C', 'Checkbox'),
        ('R', 'Radio'),
    )
    form = models.ForeignKey(Form)
    label = models.CharField(max_length=50)
    help_text = models.CharField(max_length=200, blank=True)
    type = models.CharField(max_length=1, choices=TYPE_CHOICES)
    mandatory = models.BooleanField()
    published = models.BooleanField()
    
    def __unicode__(self):
        return self.label
    
class TextField(models.Model):
    field = models.ForeignKey(Field)
    value = models.CharField(max_length=100, blank=True)
    
    def __unicode__(self):
        return self.field.label
    
class TextareaField(models.Model):
    field = models.ForeignKey(Field)
    value = models.TextField(max_length=200, blank=True)
    
    def __unicode__(self):
        return self.field.label
    
class CheckboxField(models.Model):
    field = models.ForeignKey(Field)
    
    def __unicode__(self):
        return self.field.label
    
class CheckboxFieldValue(models.Model):
    checkbox_field = models.ForeignKey(CheckboxField)
    label = models.CharField(max_length=50, blank=True)
    value = models.BooleanField()

class RadioField(models.Model):
    field = models.ForeignKey(Field)
    
    def __unicode__(self):
        return self.field.label
    
class RadioFieldValue(models.Model):
    radio_field = models.ForeignKey(RadioField)
    label = models.CharField(max_length=50, blank=True)
    value = models.BooleanField()
    
    